package com.woniu.user.controller;


import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Order;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.User;
import com.woniu.common.utils.TokenUtil;
import com.woniu.common.vo.AjaxResult;
import com.woniu.user.service.OrdersService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/Order")
@CrossOrigin
public class OrderController {
    @Resource
    private OrdersService ordersService;

    @RequestMapping("getOrder")
    public AjaxResult getOrder(HttpServletRequest request) {
        User user = TokenUtil.getObj(request, User.class);
        List<Order> order = ordersService.getOrder(user);
        return AjaxResult.success("订单如下", order);
    }

    @RequestMapping("updateOrder")
    public AjaxResult updateOrder(@Param("oid") String oid,@Param("fid") Integer fid,@Param("state") Integer state) {
        System.out.println(oid+ "**" + fid + "**" + state);
        Order order = new Order();
        order.setOid(oid);
        order.setState(state);
        Flower flower = new Flower();
        flower.setFid(fid);
        order.setFlower(flower);
        ordersService.updateOrder(order);
        return AjaxResult.success("支付状态修改完毕");
    }

    @RequestMapping("deleteOrder")
    public AjaxResult deleteOrder(@RequestBody Order order) {
        System.out.println(order + "_____++++");
        ordersService.deleteOrder(order);
        return AjaxResult.success("支付状态修改完毕");
    }

    @RequestMapping("deleteMoreOrder")
    public AjaxResult deleteOrder(@RequestBody Integer[] selectedOrderIds, HttpServletRequest request) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        ordersService.deleteOrders(selectedOrderIds, obj);
        return AjaxResult.success("订单删除完毕");
    }

    @RequestMapping("findOrder")
    public AjaxResult findOrder(@Param("username") String username, @Param("address") String address, HttpServletRequest request) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        Order order = new Order();
        order.setAddress(address);
        User user = new User();
        user.setUsername(username);
        order.setUser(user);
        order.setShops(obj);
        List<Order> orders = ordersService.getOrders(order);
        return AjaxResult.success("订单如下", orders);
    }

    @RequestMapping("isUpdate")
    public AjaxResult isUpdate(@Param("oid")String oid, HttpServletRequest request,@Param("fid")Integer fid,@Param("comment")String comment,@Param("state")Integer state) {
        User user = TokenUtil.getObj(request, User.class);
        System.out.println(oid + "**" + fid + "**" + comment+"**"+state);
        Order order = new Order();
        order.setUser(user);
        Flower flower = new Flower();
        flower.setFid(fid);
        order.setFlower(flower);
        order.setOid(oid);
        order.setComment(comment);
        order.setState(state);
        ordersService.updateOrder(order);
        return AjaxResult.success("退款申请审批成功");
    }
    @RequestMapping("submitRefunds")
    public AjaxResult submitRefunds(@Param("oid")String oid, HttpServletRequest request,@Param("fid")Integer fid,@Param("appraise")String appraise,@Param("state")Integer state,@Param("points")String points) {
        User user = TokenUtil.getObj(request, User.class);
        Order order = new Order();
        order.setUser(user);
        Flower flower = new Flower();
        flower.setFid(fid);
        order.setFlower(flower);
        order.setOid(oid);
        order.setAppraise(appraise);
        order.setState(state);
        order.setPoints(points);
        System.out.println(order);
        ordersService.updateOrder(order);
        return AjaxResult.success("退款申请审批成功");
    }

    //    增加
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody List<Order> orders) {
        System.out.println(orders + "_______++++++");
        if (orders == null || orders.isEmpty()) {
            return AjaxResult.error("订单列表为空");
        }
        Integer result = ordersService.addOrder(orders);
        return AjaxResult.success("订单添加成功");
    }
}

